package com.panfeng.xcloud.boss.provider.assets.service.impl;

import com.alibaba.fastjson.JSON;
import com.panfeng.xcloud.boss.provider.assets.dao.mapper.CoinListMapperExt;
import com.panfeng.xcloud.boss.provider.assets.dao.mapper.WalletTransFlowMapperExt;
import com.panfeng.xcloud.boss.provider.assets.dto.WalletTransFlowExtendItemDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.request.QueryWalletTransFlowPageReqDTO;
import com.panfeng.xcloud.boss.provider.assets.dto.response.QueryWalletTransFlowPageRespDTO;
import com.panfeng.xcloud.boss.provider.assets.service.IWalletTransFlowService;
import com.panfeng.xcloud.common.core.page.PageResponse;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.dao.assets.entity.CoinList;
import com.panfeng.xcloud.dao.assets.entity.WalletTransFlow;
import com.panfeng.xcloud.dao.base.MapperSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class WalletTransFlowServiceImpl extends MapperSupport<Integer, WalletTransFlow, WalletTransFlowMapperExt> implements IWalletTransFlowService {

    @Autowired
    WalletTransFlowMapperExt walletTransFlowMapperExt;

    @Autowired
    CoinListMapperExt coinListMapperExt;

    /**
     * 查询相关交易记录
     * @param queryWalletTransFlowPageReqDTO
     * @return
     */
    @Override
    public PageResponse<QueryWalletTransFlowPageRespDTO> queryWalletTransFlow(QueryWalletTransFlowPageReqDTO queryWalletTransFlowPageReqDTO) {
        PageResponse<WalletTransFlowExtendItemDTO> pageResponse = super.selectPage(queryWalletTransFlowPageReqDTO, walletTransFlowMapperExt::queryWalletTransFlowExtPageCount, walletTransFlowMapperExt::queryWalletTransFlowExtPage);

        List<WalletTransFlowExtendItemDTO> items = pageResponse.getItems();
        log.info(">>>> queryWalletTransFlow-items.list：{} <<<",(null != items ? JSON.toJSONString(items):""));
        List<QueryWalletTransFlowPageRespDTO> queryWalletTransFlowRespDTOS = ConvertUtils.convert(items, QueryWalletTransFlowPageRespDTO.class);

        List<QueryWalletTransFlowPageRespDTO> result = new ArrayList<>();

        for (QueryWalletTransFlowPageRespDTO queryWalletTransFlowRespDTO : queryWalletTransFlowRespDTOS) {
            CoinList coinList = coinListMapperExt.queryCoinListByDestinationCoinAndSourceCoin(queryWalletTransFlowRespDTO.getExchangeType(),
                    queryWalletTransFlowRespDTO.getDestinationCoin(),queryWalletTransFlowRespDTO.getSourceCoin(), null,
                    queryWalletTransFlowRespDTO.getDirection());
            queryWalletTransFlowRespDTO.setUrl(coinList.getUrl());
            result.add(queryWalletTransFlowRespDTO);
        }

        PageResponse<QueryWalletTransFlowPageRespDTO> pageFinalResponse = new PageResponse();
        pageFinalResponse.setTotalCount(pageResponse.getTotalCount());
        pageFinalResponse.measureTotalPage(pageResponse.getTotalCount(),pageResponse.getPageSize());
        pageFinalResponse.setItems(result);
        pageFinalResponse.setPageNumber(pageResponse.getPageNumber());
        pageFinalResponse.setPageSize(pageResponse.getPageSize());

        return pageFinalResponse;
    }

}
